﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _11_ProductOfConsecutiveFibNumbers
{
    public class ProdFib
    {
        public static ulong[] productFib(ulong prod)
        {
            if (prod == 0)
                return new ulong[] { 1, 1, 0 };
            if (prod == 1)
                return new ulong[] { 1, 1, 1 };
            ulong a = 1;
            ulong b = 1;
            ulong c = 1;
            ulong num = 0;
            for (int i = 3; num < prod; i++)
            {
                c = checked(a + b); // when n>46 memory will overflow
                a = b;
                b = c;
                num = (ulong)(a * b);
            }
            return new ulong[] { a, b, Convert.ToUInt32(num == prod) };

            //2、斐波拉契数列的写法
            //ulong a = 0, b = 1, temp;
            //while (a * b < prod)
            //{
            //    b = a + b;
            //    a = b - a;
            //    if (a * b == prod)
            //        return new ulong[] { a, b, 1 };
            //}
            //return new ulong[] { a, b, 0 };
        }
    }
}
